<?php

if(!defined('_LIB_SEARCH_')) {
define('_LIB_SEARCH_', 1);

include  _ITSTOCK_LIB_DIR_.'db.inc.php';

function itstock_search(&$data, $query, $idx = 'data') {
  $found = false;
  $data[$idx] = array();
  itstock_db_connect($db);
  if (!itstock_db_error($db)) {
    if (itstock_db_query($db, $query)) {
      for ($i = 0; itstock_db_fetch_assoc($db); $i++) {
        $data[$idx][$i] = itstock_db_row($db);
        $found = true;
      }
      itstock_db_free($db);
    } else {
      $data['msg'] = itstock_db_error($db);
      $data['db_errno'] = $db['errno'];;
    }
  } else {
    $data['db_errno'] = $db['errno'];
    $data['msg'] = $db['error'];
    $found = false;
  }
  return $found;
}

function itstock_search_all(&$data, $idx = 'data', $debug = false) {
  $no_union = false;
  if(!array_key_exists('union',$data)){
    $data['union'][0] = &$data;
    $no_union = true;
  }

  $query = false;

  foreach($data['union'] as &$select) {

  $query .= !$query?'':' UNION ALL ';
  $alias = "*, ".$select['table'].".id";
  $joins = "";
  $where = "";
  $order = "";
  $limit = "";

  if (array_key_exists('alias', $select)) {
    $alias = implode(', ', $select['alias']);
  }
  if (array_key_exists('joins', $select) && array_key_exists('join_fields', $select)) {
    $joins = sprintf('LEFT JOIN (%s) ON (%s)', implode(', ', $select['joins']), implode(' AND ', $select['join_fields']));
  }
  if (array_key_exists('search', $select) && $select['search'] !== '') {
    $where = 'WHERE (`' . implode('` LIKE \'%'.$select['search'].'%\' OR `', $select['search_fields']) . '` LIKE \'%'.$select['search'].'%\' )';
  }
  if (array_key_exists('where', $select) && !empty($select['where'])) {
    $where = ($where == ""? 'WHERE ' : $where . ' AND ') . implode(' AND ', $select['where']);
  }
  if (!(array_key_exists('sort', $select))) {
    $select['sort'] = 'ASC';
  }
  if (array_key_exists('order', $select)) {
    $order = sprintf('ORDER BY %s %s', $select['order'], $select['sort']);
  }
  if (!(array_key_exists('offset', $select))) {
    $select['offset'] = 0;
  }
  if (!(array_key_exists('rows', $select))) {
    $select['rows'] = _ITSTOCK_MAX_ROWS_;
  }
  if (!array_key_exists('limit', $select)) {
    $limit = sprintf('LIMIT %s, %s', $select['offset'], $select['rows']);
  }
  $query .= sprintf('SELECT %2$s FROM %1$s %3$s %4$s %5$s %6$s', $select['table'], $alias , $joins, $where, $order, $limit);

  }
  if($no_union) unset($data['union']);
  if($debug) echo $query;
  return itstock_search($data, $query, $idx);
}

function itstock_search_count(&$data, $debug = false){

  $no_union = false;
  if(!array_key_exists('union',$data)){
    $data['union'][0] = &$data;
    $no_union = true;
  }
  foreach($data['union'] as $i => &$select) {
    $tmp_alias[$i] = (isset($select['alias'])? $select['alias']: null);
    $tmp_limit[$i] = (isset($select['limit'])? $select['limit']: null);
    $select['alias'] = array("COUNT(*) AS 'total'");
    $select['limit'] = '';
  }
  $result = itstock_search_all($data, 'total', $debug);
  foreach($data['union'] as $i => &$select) {
    if (!is_null($tmp_alias[$i])) $select['alias'] = $tmp_alias[$i]; else unset($select['alias']);
    if (!is_null($tmp_limit[$i])) $select['limit'] = $tmp_limit[$i]; else unset($select['limit']);
  }
  if($no_union) unset($data['union']);
  return $result;
}

}
?>
